package cn.qihang.ssm.controller;

import cn.qihang.ssm.pojo.Role;
import cn.qihang.ssm.pojo.UserInfo;
import cn.qihang.ssm.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * @Author: qihang
 * @Date: 2022/11/3 11:36
 * @Desc:
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 查询所有用户
     * @param page
     * @param size
     * @return
     */
    @RolesAllowed("ADMIN")
    // @Secured("ROLE_ADMIN")
    // @PreAuthorize("authentication.principal.username == 'admin'")
    @RequestMapping("findAll")
    public ModelAndView findAll(
            @RequestParam(value = "page",defaultValue = "1") Integer page ,
            @RequestParam(value = "size" , defaultValue = "5") Integer size
    ){

        PageHelper.startPage(page,size);
        List<UserInfo> userList = this.userService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(userList);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo" , pageInfo);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }


    /**
     * 新增用户
     * @param userInfo
     * @return
     */
    @RequestMapping("save")
    public String save(UserInfo userInfo){
        this.userService.save(userInfo);
        return "redirect:findAll.do";
    }


    /**
     * 用户详情
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public ModelAndView findById(@RequestParam("id") Integer id){
        ModelAndView modelAndView = new ModelAndView();
        UserInfo userInfo = this.userService.findById(id);
        modelAndView.addObject("user" , userInfo);
        modelAndView.setViewName("user-show");
        return modelAndView;
    }

    /**
     * 添加角色前-查询当前用户哪些角色可以添加
     * @param id
     * @return
     */
    @RequestMapping("findUserByIdAndAllRole")
    public ModelAndView findUserByIdAndAllRole(@RequestParam("id") Integer id){
        //添加用户前，得知道你添加的用户是谁
        UserInfo userInfo = this.userService.findUserById(id);

        //根据用户查询哪些角色是可以添加（除了本身以经有的角色以外的才可以添加）
        List<Role> otherRoles = this.userService.findOtherRoles(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user" ,userInfo);
        modelAndView.addObject("roleList",otherRoles);
        modelAndView.setViewName("user-role-add");
        return modelAndView;
    }


    /**
     * 给用户添加角色
     * @param userId
     * @param ids
     * @return
     */
    @RequestMapping("addRoleToUser")
    public String addRoleToUser(@RequestParam("userId") Integer userId,
                                @RequestParam("ids") Integer[] ids){
        this.userService.addRoleToUser(userId, ids);
        return "redirect:findAll.do";
    }


    /**
     * 删除用户
     * @return
     */
    @RequestMapping("deleteUser")
    public String deleteUser(@RequestParam("id") Integer id){
        this.userService.deleteUser(id);
        return "redirect:findAll.do";
    }
}
